#include "Panels.h"

#include "data\slide_show.hpp"
#include "Communicate.h"
#include <resource_cache\resource_cache.hpp>
#include "string_conv.h"
#include "UI\interface_ids.hpp"
#include "shortcuts.hpp"

#define BODYUI m_slide_data.m_body_ui
#define SHAPEUI m_slide_data.m_shape_ui

void LeftPanel::clear_shape_ui()
{
}
void LeftPanel::set_to_current_shape_ui()
{
	if(!active_shape())
		return;
	SHAPEUI.m_circle_offset_x.set_text( stringify(CSHAPE.circle_offset.x) );
	SHAPEUI.m_circle_offset_y.set_text( stringify(CSHAPE.circle_offset.y) );
	SHAPEUI.m_circle_radius.set_text( stringify(CSHAPE.circle_radius) );
	SHAPEUI.m_collision_message.set_text( CSHAPE.on_collide_message );
	SHAPEUI.m_density.set_text( stringify(CSHAPE.density) );
	SHAPEUI.m_friction.set_text( stringify(CSHAPE.friction) );
	SHAPEUI.m_is_sensor->SetValue( CSHAPE.is_sensor );
	SHAPEUI.m_restitution.set_text( stringify(CSHAPE.restitution) );
	SHAPEUI.m_vertices->Clear();
	for( unsigned int i = 0; i < CSHAPE.polygon_points.size(); ++i )
	{
		std::string v_name;
		v_name.append( "x:" );
		v_name.append( stringify(CSHAPE.polygon_points[i].x) );
		v_name.append( " y:" );
		v_name.append( stringify(CSHAPE.polygon_points[i].y) );
		SHAPEUI.m_vertices->Append(v_name.c_str());
	}

	bool a = true;
	bool b = false;
	if( CSHAPE.shape_type == shape_polygon )
	{
		a=!a; b=!b;
	}
	SHAPEUI.m_active_circle_offset->Show(a);
	SHAPEUI.m_circle_offset_x.Show(a);
	SHAPEUI.m_circle_offset_y.Show(a);
	SHAPEUI.m_circle_radius.Show(a);
	SHAPEUI.m_active_circle_offset->Show(a);

	SHAPEUI.m_append_vertex->Show(b);
	SHAPEUI.m_active_vertex_position->Show(b);
	SHAPEUI.m_delete_vertex->Show(b);
	SHAPEUI.m_vertices->Show(b);
	SHAPEUI.m_vertex_x.Show(b);
	SHAPEUI.m_vertex_y.Show(b);

	SHAPEUI.m_switch_polygon->Enable(a);
	SHAPEUI.m_switch_circular->Enable(b);
}

void LeftPanel::on_new_shape(wxCommandEvent & WXUNUSED(e))
{
	if(!active_body())
		return;

	shape new_shape;
	SHAPES.push_back( new_shape );
	SHAPEUI.m_shapes->Append( wxT("New Shape" ) );
}
void LeftPanel::on_delete_shape(wxCommandEvent & WXUNUSED(e))
{
	if( !active_shape() )
		return;

	SHAPEUI.m_shapes->Delete( CBODY.current_shape );
	SHAPES.erase( SHAPES.begin() + CBODY.current_shape );
}
void LeftPanel::on_switch_circular(wxCommandEvent & WXUNUSED(e))
{
	if(!active_shape())
		return;

	CSHAPE.shape_type = shape_circle;

	SHAPEUI.m_active_circle_offset->Show(true);
	SHAPEUI.m_circle_offset_x.Show(true);
	SHAPEUI.m_circle_offset_y.Show(true);
	SHAPEUI.m_circle_radius.Show(true);
	SHAPEUI.m_active_circle_offset->Show(true);

	SHAPEUI.m_append_vertex->Show(false);
	SHAPEUI.m_active_vertex_position->Show(false);
	SHAPEUI.m_delete_vertex->Show(false);
	SHAPEUI.m_vertices->Show(false);
	SHAPEUI.m_vertex_x.Show(false);
	SHAPEUI.m_vertex_y.Show(false);

	SHAPEUI.m_switch_polygon->Enable(true);
	SHAPEUI.m_switch_circular->Enable(false);
}
void LeftPanel::on_switch_polygonal(wxCommandEvent & WXUNUSED(e))
{
	if(!active_shape())
		return;

	CSHAPE.shape_type = shape_polygon;

	SHAPEUI.m_active_circle_offset->Show(false);
	SHAPEUI.m_circle_offset_x.Show(false);
	SHAPEUI.m_circle_offset_y.Show(false);
	SHAPEUI.m_circle_radius.Show(false);
	SHAPEUI.m_active_circle_offset->Show(false);

	SHAPEUI.m_append_vertex->Show(true);
	SHAPEUI.m_active_vertex_position->Show(true);
	SHAPEUI.m_delete_vertex->Show(true);
	SHAPEUI.m_vertices->Show(true);
	SHAPEUI.m_vertex_x.Show(true);
	SHAPEUI.m_vertex_y.Show(true);

	SHAPEUI.m_switch_polygon->Enable(false);
	SHAPEUI.m_switch_circular->Enable(true);
}
void LeftPanel::on_append_vertex(wxCommandEvent & WXUNUSED(e))
{
	if(!active_shape() )
		return;
	b2Vec2 vert(0,0);
	CSHAPE.polygon_points.push_back(vert);
	std::string poly_name;
	poly_name.append( "x:" );
	poly_name.append( stringify(vert.x) );
	poly_name.append( " y:" );
	poly_name.append( stringify(vert.y) );
	SHAPEUI.m_vertices->Append(poly_name.c_str());
}
void LeftPanel::on_delete_vertex(wxCommandEvent & WXUNUSED(e))
{
	if(!active_vertex())
		return;

	SHAPEUI.m_vertices->Delete( CSHAPE.current_polygon );
	CSHAPE.polygon_points.erase( CSHAPE.polygon_points.begin() + CSHAPE.current_polygon );
}
void LeftPanel::on_active_vertex(wxCommandEvent & WXUNUSED(e))
{
	if(!active_vertex())
		return;

	g_slides.active_vertex_pos = !g_slides.active_vertex_pos;
	if( g_slides.active_vertex_pos )
	{
		SHAPEUI.m_active_vertex_position->SetLabel( "ACTIVE" );
		SHAPEUI.m_active_vertex_position->SetBackgroundColour( wxColour( "blue" ) );
	}
	else
	{
		SHAPEUI.m_active_vertex_position->SetLabel( "inactive" );
		SHAPEUI.m_active_vertex_position->SetBackgroundColour( wxColour( "none" ) );
	}
}
void LeftPanel::on_active_circle(wxCommandEvent & WXUNUSED(e))
{
	if(!active_shape())
		return;

	g_slides.active_circle_pos = !g_slides.active_circle_pos;
	if( g_slides.active_circle_pos )
	{
		SHAPEUI.m_active_circle_offset->SetLabel( "ACTIVE" );
		SHAPEUI.m_active_circle_offset->SetBackgroundColour( wxColour( "blue" ) );
	}
	else
	{
		SHAPEUI.m_active_circle_offset->SetLabel( "inactive" );
		SHAPEUI.m_active_circle_offset->SetBackgroundColour( wxColour( "none" ) );
	}
}
void LeftPanel::on_switch_shape(wxCommandEvent & WXUNUSED(e))
{
	if( !active_body() )
		return;
	int selection = SHAPEUI.m_shapes->GetSelection();
	CBODY.current_shape = selection;
	set_to_current_shape_ui();
}
void LeftPanel::on_switch_vertex(wxCommandEvent & WXUNUSED(e))
{
	if( !active_shape() )
		return;
	int selection = SHAPEUI.m_vertices->GetSelection();
	CSHAPE.current_polygon = selection;
	
	SHAPEUI.m_circle_offset_x.set_text( stringify(CSHAPE.circle_offset.x) );
	SHAPEUI.m_circle_offset_y.set_text( stringify(CSHAPE.circle_offset.y) );
	SHAPEUI.m_circle_radius.set_text( stringify(CSHAPE.circle_radius) );
	SHAPEUI.m_collision_message.set_text( CSHAPE.on_collide_message );
	SHAPEUI.m_density.set_text( stringify(CSHAPE.density) );
	SHAPEUI.m_friction.set_text( stringify(CSHAPE.friction) );
	SHAPEUI.m_is_sensor->SetValue( CSHAPE.is_sensor );
	SHAPEUI.m_restitution.set_text( stringify(CSHAPE.restitution) );
	SHAPEUI.m_vertex_x.set_text( stringify(CSHAPE.polygon_points[CSHAPE.current_polygon].x) );
	SHAPEUI.m_vertex_y.set_text( stringify(CSHAPE.polygon_points[CSHAPE.current_polygon].y) );
}
void LeftPanel::on_change_density(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.density = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_restitution(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.restitution = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_friction(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.friction = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_collision_message(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.on_collide_message = e.GetString().char_str();
}
void LeftPanel::on_change_vertex_x(wxCommandEvent & e)
{
	if(!active_vertex())
		return;
	CSHAPE.polygon_points[CSHAPE.current_polygon].x = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_vertex_y(wxCommandEvent & e)
{
	if(!active_vertex())
		return;
	CSHAPE.polygon_points[CSHAPE.current_polygon].y = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_circle_x(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.circle_offset.x = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_circle_y(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.circle_offset.y = ::atof(e.GetString().char_str());
}
void LeftPanel::on_change_radius(wxCommandEvent & e)
{
	if(!active_shape())
		return;
	CSHAPE.circle_radius = ::atof(e.GetString().char_str());
}
void LeftPanel::on_check_sensor(wxCommandEvent & WXUNUSED(e))
{
	if(!active_shape())
		return;
	CSHAPE.is_sensor = SHAPEUI.m_is_sensor->GetValue();
}

